<?php
App::uses('AppController', 'Controller');
/**
 * Users Controller
 *
 * @property User $User
 * @property PaginatorComponent $Paginator
 * @property SessionComponent $Session
 */
class UsersController extends AppController {

/**
 * Components
 *
 * @var array
 */
	public $components = array('Paginator', 'Session');

/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->User->recursive = 2;
		$this->set('userLogins', $this->Paginator->paginate());
	}

	public function admin_index() {
		$this->User->recursive = 2;
		$this->set('users', $this->Paginator->paginate());
	}

	// public function admin_index() {
	//     $this->User->recursive = 0;
 //        $this->paginate=array('limit'=>10);
	// 	$users =$this->paginate('User');
 //        $this->set(compact('users'));
	// }

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->User->exists($id)) {
			throw new NotFoundException(__('Invalid user login'));
		}
		$options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
		$this->set('userLogin', $this->User->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
	public function admin_add() {

		if ($this->request->is('post')) {

			$this->User->create();
			// var_dump($this->request->data);
			$this->request->data['User']['password']=md5($this->request->data['User']['password']);
			if ($this->User->save($this->request->data)) {
				$this->Session->setFlash(__('The user login has been saved.'));
				return $this->redirect(array('action' => 'admin_index'));
			} else {
				debug($this->User->invalidFields());
				$this->Session->setFlash(__('The user login could not be saved. Please, try again.'));
			}
		}
                                                   $this->loadModel('MrCode');
                                    $mrCode = $this->MrCode->find('list',array('fields'=>array('id','name_code')));
                                    $this->set(compact('mrCode'));
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_edit($id = null) {
		// var_dump($id);
		if (!$this->User->exists($id)) {
			throw new NotFoundException(__('Invalid user login'));
		}
		if ($this->request->is(array('post', 'put'))) {
			$this->request->data['User']['password']=md5($this->request->data['User']['password']);
			if ($this->User->save($this->request->data)) {
				$this->Session->setFlash(__('The user login has been saved.'));
				return $this->redirect(array('action' => 'admin_index'));
			} else {
				$this->Session->setFlash(__('The user login could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
			$this->request->data = $this->User->find('first', $options);
		}
                                                  $this->loadModel('MrCode');
                                                  $mrCode = $this->MrCode->find('list',array('fields'=>array('id','name_code')));
                                                  $this->set(compact('mrCode'));
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_delete($id = null) {
		$this->User->id = $id;
		if (!$this->User->exists()) {
			throw new NotFoundException(__('Invalid user login'));
		}
		if ($this->User->delete()) {
			$this->Session->setFlash(__('The user login has been deleted.'));
		} else {
			$this->Session->setFlash(__('The user login could not be deleted. Please, try again.'));
		}
		return $this->redirect(array('action' => 'admin_index'));
	}

	public function admin_login()
    {
        $this->layout="backend/login";
        $data = $this->request->data;
		 //var_dump(phpinfo());
            
        if(!empty($data))
        {
        	$uname = $data['Users']['user'];
            $pass  = $data['Users']['password'];
            $users = $this->User->find('first',array('conditions'=>array('User.user'=>$uname,'User.password'=>md5($pass))));
            if(count($users) >0)
            {
                $idUser    = $users['User']['id'];
                $lastLogin = date("Y-m-d H:i:s");

                $this->Session->write('userId',$idUser);
                $this->Session->write('userName',$uname);
                $this->Session->write('loginTime',$lastLogin);
                $this->User->id = $idUser;
                $this->User->set('last_time',$lastLogin);
                $this->redirect("/pages/admin_index");
                //$this->Session->setFlash('Login successfull ','',array('class'=>false));
            }
            else {
                $this->Session->setFlash('Invalid email or password','',array('class'=>false));
            }
        }
        else{
            $this->Session->setFlash('Enter email and password','',array('class'=>false));
        }       
    }
    public function admin_logout()
    {
        $this->Session->destroy();
        $this->redirect("/");
    }
}
